import {defineConfig, loadEnv} from 'vite'
import path from 'path'
import createVitePlugins from './vite/plugins'
import {visualizer} from 'rollup-plugin-visualizer'

// https://vitejs.dev/config/

export default defineConfig(({mode, command}) => {
    const env = loadEnv(mode, process.cwd())
    return {
        base: '/',
        build: {
            outDir: 'dist',
            rollupOptions: {
                plugins: [visualizer({json: false})]
            },
        },
        plugins: createVitePlugins(env, command === 'build'),

        resolve: {
            // https://cn.vitejs.dev/config/#resolve-alias
            alias: {
                // 设置路径
                '~': path.resolve(__dirname, './'),
                // 设置别名
                '@': path.resolve(__dirname, './src')
            },
            // https://cn.vitejs.dev/config/#resolve-extensions
            extensions: ['.mjs', '.js', '.ts', '.jsx', '.tsx', '.json', '.vue']
        },
        css: {
            postcss: {
                plugins: [
                    {
                        postcssPlugin: 'internal:charset-removal',
                        AtRule: {
                            charset: (atRule) => {
                                if (atRule.name === 'charset') {
                                    atRule.remove();
                                }
                            }
                        }
                    }
                ],
            },
        },
        // vite 相关配置
        server: {
            port: 83,
            //host: true,
            open: true,
            proxy: {
                // https://cn.vitejs.dev/config/#server-proxy
                '/dev-api': {
                    target: 'http://vue.ruoyi.vip/prod-api/',
                    changeOrigin: true,
                    rewrite: (p) => p.replace(/^\/dev-api/, '')
                },
                '/prod-api': {
                    target: 'http://vue.ruoyi.vip/prod-api/',
                    changeOrigin: true,
                    rewrite: (p) => p.replace(/^\/prod-api/, '')
                }
            },
        },
        preview: {
            port: 84,
            //host: true,
            open: true,
            proxy: {
                // https://cn.vitejs.dev/config/#server-proxy
                '/dev-api': {
                    target: 'http://vue.ruoyi.vip/prod-api/',
                    changeOrigin: true,
                    rewrite: (p) => p.replace(/^\/dev-api/, '')
                },
                '/prod-api': {
                    target: 'http://vue.ruoyi.vip/prod-api/',
                    changeOrigin: true,
                    rewrite: (p) => p.replace(/^\/prod-api/, '')
                }
            },
        },
    }
})
